package com.yulr.demo.http;

import android.app.Activity;
import android.os.Bundle;

import org.apache._http.HttpRequest;
import org.apache._http.HttpResponse;
import org.apache._http.entity.ContentType;
import org.apache._http.nio.entity.NFileEntity;
import org.apache._http.nio.entity.NStringEntity;
import org.apache._http.protocol.HttpContext;

import java.io.File;

import cn.yulr.http.HttpRequestActionListener;
import cn.yulr.http.HttpRequestAsyncRequestHander;
import cn.yulr.http.MHttpServer;
import cn.yulr.system.resource.SystemResource;

public class MainActivity extends Activity {

    private final MHttpServer mHttpServer = new MHttpServer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int max = SystemResource.getInstance().getrlimit_max(SystemResource.RLIMIT_NOFILE);
        SystemResource.getInstance().setrlimit(SystemResource.RLIMIT_NOFILE, max, max);


        HttpRequestAsyncRequestHander contentTesting = new HttpRequestAsyncRequestHander("/content");
        contentTesting.addHttpRequestActionFinishedListener(new HttpRequestActionListener() {
            @Override
            public boolean onHttpRequestAction(String requestAction, HttpRequest httpRequest, HttpResponse httpResponse, HttpContext context) {
                System.out.println("响应结果");
                NStringEntity body = new NStringEntity("ABCDEFGHIJKLMN", ContentType.create("application/octet-stream"));
                httpResponse.setEntity(body);
                return true;
            }

            @Override
            public void onHttpRequestActionPreSend(String requestAction, HttpRequest httpRequest, HttpResponse httpResponse, HttpContext context) {
                System.out.println("准备发送响应结果");
            }

            @Override
            public void onHttpRequestActionFinished(String requestAction, HttpRequest httpRequest, HttpResponse httpResponse, HttpContext context) {
                System.out.println("响应结束");
            }
        });
        mHttpServer.add(contentTesting);
        System.out.println("启动http服务器");
        mHttpServer.initHttpServer(8085, "192.168.1.104", "cktor-server");

    }

    @Override
    protected void onStop() {
        super.onStop();
        mHttpServer.releaseHttpServer();
    }
}
